package com.sww.pj.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sww.pj.common.annotation.RequiredLog;
import com.sww.pj.common.exception.ServiceException;
import com.sww.pj.common.pojo.PageObject;
import com.sww.pj.common.utils.AssertUtils;
import com.sww.pj.sys.dao.SysWlUserDao;
import com.sww.pj.sys.pojo.SysWlUser;
import com.sww.pj.sys.service.SysWlUserService;
@Service
public class SysWlUserServiceImpl implements SysWlUserService {

	@Autowired
	private SysWlUserDao sysWlUserDao;

	@Override
	@RequiresPermissions("sww:wluser:view")
	@RequiredLog("查询外来用户")
	public PageObject<SysWlUser> findPageObject(String name, Long pageCurrent) {
		//1.验证参数合法性
		//1.1验证pageCurrent的合法性，
		//不合法抛出IllegalArgumentException异常
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页码不正确");
		//2.基于条件查询总记录数
		//2.1) 执行查询
		long rowCount=sysWlUserDao.getRowCount(name);
		//2.2) 验证查询结果，假如结果为0不再执行如下操作
		if(rowCount==0)
			throw new ServiceException("系统没有查到对应记录");
		//3.基于条件查询当前页记录(pageSize定义为2)
		//3.1)定义pageSize
		int pageSize=10;
		//3.2)计算startIndex
		Long startIndex=(long) ((pageCurrent-1)*pageSize);
		//3.3)执行当前数据的查询操作
		List<SysWlUser> records=
				sysWlUserDao.findPageObject(name, startIndex, pageSize);
		//4.对分页信息以及当前页记录进行封装
		//4.1)构建PageObject对象
		PageObject<SysWlUser> pageObject=new PageObject<>();
		//4.2)封装数据
		pageObject.setPageCurrent(pageCurrent);
		pageObject.setPageSize(pageSize);
		pageObject.setRowCount(rowCount);
		pageObject.setRecords(records);
		pageObject.setPageCount((rowCount-1)/pageSize+1);
		//5.返回封装结果。
		return pageObject;
	}

	//删除操作
	@Override
	@RequiredLog("删除外来用户")
	public int deleteObject(Integer... ids) {
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("请输入id的值");
		int rows=sysWlUserDao.deleteObject(ids);
		if(rows==0)
			throw new ServiceException("记录可能已经不存在");
		return rows;
	}

	//添加操作
	@Override
	@RequiredLog("添加外来用户")
	public int saveObject(SysWlUser entity, Integer[] roleIds) {
		//1.参数校验
		AssertUtils.isArgValid(entity==null, "保存对象不能为空");
		AssertUtils.isArgValid(entity.getUsername()==null||"".equals(entity.getUsername()), "用户名不能为空");
		int rows=sysWlUserDao.insertObject(entity);	
		return rows;
	}

	//修改前的查询
	@Override
	public Map<String, Object> findObjectById(Integer id) {
		// TODO Auto-generated method stub
		//1.参数校验
		AssertUtils.isArgValid(id==null||id<1,"id值无效");
		//2.查询用户以及用户对应的信息
		SysWlUser user=sysWlUserDao.findObjectById(id);
		AssertUtils.isServiceValid(user==null, "记录可能已经不存在");

		//4.封装查询结果
		Map<String,Object> map=new HashMap<>();
		map.put("user", user);

		//long t2=System.currentTimeMillis();
		//log.info("time {}",t2-t1);
		return map;
	}

	@Override
	@RequiredLog("修改外来用户")
	public int updateObject(SysWlUser entity, Integer[] roleIds) {
		// TODO Auto-generated method stub
		//1.参数校验
		AssertUtils.isArgValid(entity==null, "保存对象不能为空");
		AssertUtils.isArgValid(entity.getUsername()==null||"".equals(entity.getUsername()), "用户名不能为空");

		//2.保存用户自身信息
		int rows=sysWlUserDao.updateObject(entity);
		AssertUtils.isServiceValid(rows==0, "记录可能已经不存在了");

		return rows;
	}
}


